import {
  adminSystemIndexAPI
} from '/src/api/admin/config.js'
import {
  crmSettingConfigDataAPI
} from '/src/api/admin/crm.js'
import {
  adminConfigsetIndexAPI
} from '/src/api/admin/application.js'
import {configHeaderModelSortAPI} from '/src/api/config.js'
import Lockr from 'lockr'
import {defineStore} from "pinia";

/** 记录 侧边索引 */
export const useAppStore = defineStore('app', {
  state: () => ({
    logo: '',
    name: '',
    lang: localStorage.lang || 'cn',
    sidebar: {
      activeIndex: '', // 目前激活的 行
      collapse: Lockr.get('sideBarCollapse') || false
    },
    navbar: {
      activeIndex: '' // 导航目前是第几个 个人中心需要
    },
    // CRM配置信息
    CRMConfig: {},
    // 图片缓存
    imageCache: {},
    headerModule: null, // 置顶模块
    // 模块权限
    moduleAuth: null,
    // 为了在FixiOSBug中使用，添加了一个变量
    device: ""
  }),
  getters: {
    logoGet: state => {
      if (state.app.logo) {
        return state.app.logo
      }
      return require('/src/assets/img/logo.png')
    },
    collapse: state => state.sidebar.collapse,
    activeIndex: state => state.sidebar.activeIndex,
    navActiveIndex: state => state.navbar.activeIndex,

  },
  actions: {
    SET_ACTIVEINDEX(path) {
      this.sidebar.activeIndex = path
    },
    SET_COLLAPSE(collapse) {
      this.sidebar.collapse = collapse
      Lockr.set('sideBarCollapse', collapse)
    },
    SET_NAVACTIVEINDEX( path) {
      // this.navbar.activeIndex = path
    },
    SET_APPLOGO( logo) {
      this.logo = logo
    },
    SET_APPNAME( name) {
      this.name = name
    },
    SET_LANG( lang) {
      this.lang = lang
      window.app.$i18n.locale = lang
      localStorage.setItem('lang', lang)
      window.location.reload()
    },
    SET_CRMCONFIG( config) {
      this.CRMConfig = config
    },
    SET_IMAGECACHE( value) {
      this.imageCache = value
    },
    SET_MODULEAUTH( value) {
      this.moduleAuth = value
    },
    SET_HEADERMODULE( value) {
      this.headerModule = value
    },

    // 登录
    async SystemLogoAndName() {
      return new Promise((resolve, reject) => {
        adminSystemIndexAPI().then(response => {
          console.log("response",response)
          const resData = response.data.data || {}
          this.SET_APPNAME(resData.companyName)
          this.SET_APPLOGO(resData.companyLogo)
          Lockr.set('systemLogo', resData.companyLogo)
          Lockr.set('systemName', resData.companyName)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    /**
     * 获取客户管理配置
     */
    async CRMSettingConfig() {
      return new Promise((resolve, reject) => {
        crmSettingConfigDataAPI().then(response => {
          this.SET_CRMCONFIG(response.data)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 置顶应用
    async WKHeaderModule() {
      return new Promise((resolve, reject) => {
        configHeaderModelSortAPI().then(response => {
          this.SET_HEADERMODULE(response.data || [])
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 查询模块权限
    async QueryModules() {
      return new Promise((resolve, reject) => {
        adminConfigsetIndexAPI().then(res => {
          const resData = res.data || []
          const auth = {}
          resData.forEach(item => {
            auth[item.module] = item.status === 1
          })
          this.SET_MODULEAUTH(auth)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
})
